<template>
	<view class="container">
		<view class="cropper">
			<bt-cropper v-if="imageSrc" ref="croper" :ratio="ratio" :dWidth="400" :rotate="rotate" fileType="png" showGrid @change="onChange"
				:imageSrc="imageSrc" :mask="mask">
			</bt-cropper>
		</view>
		<view class="footer">
			<scroll-view :scroll-x="true" class="scroller">
				<view class="scrollerContainer">
					<view @click="ratio=0" class="item" :class="{
						active: ratio==0
					}">
						<view class="itemContent ratio-1-1">
							自由
						</view>
					</view>
					<view v-for="(item,index) in ratioList" :key="index" @click="chooseRatio(index)" class="item"
						:class="{
						active:activeIndex===index && ratio!==0
					}">
						<view :class="'itemContent ratio-' + item.width + '-' + item.height">
							{{item.width}}:{{item.height}}
						</view>
					</view>
				</view>
			</scroll-view>
			<view class="btnGroup">
				<view class="btn choose" @click="chooseImage">
					选择图片
				</view>
				<view class="btn" @click="onCrop">
					开始裁切
				</view>
			</view>
			<slider @changing="onRotate"></slider>
		</view>
	</view>
</template>

<script>
	import $http from '@/common/api/request.js'
	export default {
		data() {
			return {
				baseurl:"https://zyc618.mynatapp.cc",
				imageSrc: "",
				userInfo:0,
				mask: "",
				patharr:[],
				dWidth: 500,
				rotate: 0,
				// 输出的宽高比例
				// ratio: 3 / 2,
				ratio: 1 / 1,
				activeIndex: 0,
				ratioList: [{
					width: 1,
					height: 1,
				}, {
					width: 16,
					height: 9,
				}, {
					width: 9,
					height: 16,
				}, {
					width: 4,
					height: 3,
				}, {
					width: 3,
					height: 4,
				}, {
					width: 3,
					height: 2,
				}, {
					width: 2,
					height: 3,
				}]
			}
		},
		onLoad(options) {
			this.imageSrc = decodeURIComponent(options.src)
			this.userInfo = options.userid
		},
		mounted() {},
		methods: {
			onChange(ev) {
				console.log(ev)
			},
			chooseRatio(index) {
				this.activeIndex = index
				let item = this.ratioList[index]
				this.ratio = item.width / item.height
			},
			chooseImage() {
				uni.chooseImage({
					count: 1,
					sizeType: ['original'],
					success: (res) => {
						let tempFilePaths = res.tempFilePaths[0]
						this.imageSrc = tempFilePaths
					}
				})
			},
			onCrop(cb) {
				this.$refs.croper.crop().then(path => {
					//查看图片:
					uni.previewImage({
						urls:[path]
					})
					
					this.saveimgfun(path)
					//进行页面跳转返回我的页面
					this.$emit('getbigimg',path);
					
				})
			},
			onRotate(ev){
				this.rotate = 360 * (ev.detail.value / 100)
			},
			//上传图片命名日期
			formattedDate() {
				  const rawDate = new Date();
			      const year = rawDate.getFullYear();
			      const month = (rawDate.getMonth() + 1).toString().padStart(2, '0');
			      const day = rawDate.getDate().toString().padStart(2, '0');
			      return `${year}${month}${day}`;
			    },
			//图片上传服务器:
			saveimgfun(pathfile){
				const uploadTask=uni.uploadFile({
					url: this.baseurl+'/api/files/upload', //仅为示例，非真实的接口地址
					filePath:pathfile,
					name: 'file',
					formData: {
						url: this.formattedDate(),
						pathname:this.userInfo + Date.now()
					},
					success: (uploadFileRes) => {
						let imgdatalist =JSON.parse(uploadFileRes.data);
						
						if(imgdatalist.code == 200){
							
							//获取后端处理图片地址信息,完成信息修改
							this.savemsg(imgdatalist.data.url);
						}
					}
				});
				uploadTask.onProgressUpdate((res) => {
					console.log('上传进度' + res.progress);
					console.log('已经上传的数据长度' + res.totalBytesSent);
					console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
				});
			},
			
			savemsg(imgurl){
				$http.request({
					url: "/user/update",
					method: "POST",
					header:{
						token:true
					},
					data: {
						content: imgurl,
						wherelistid:28,
						userid:this.userInfo
					}
				}).then((res) => {
					if(res.code === 200){
						//成功修改跳转回我的修改
						setTimeout(()=>{
							this.navigateTo({
								url:`/pages/my-config/my-config`
							})
						},200)
					}else{
						uni.showToast({
							title:"更新异常",
							icon:"none",
							during:2500,
							mask:true,
							icon:"error"
						})
					}
					
				}).catch((err) => {
					console.log(err);
				})
				
			},
		},
	}
</script>

<style lang="scss" scoped>
	.container {
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		// #ifndef H5
		height: 100vh;
		// #endif
		// #ifdef H5
		height: 100%;
		// #endif
		background-color: #000;
		.cropper{
			flex: 1;
		}
	}
	
	uni-page-body {
		height: 100%;
	}

	.footer {
		.scroller {
			// align-items: center;
			width: 100vw;
			height: 100rpx;
			touch-action: none;

			.scrollerContainer {
				display: flex;
				flex-wrap: nowrap;
				height: 100rpx;
				align-items: center;
			}

			.item {
				display: flex;
				justify-content: center;
				align-items: center;
				margin-left: 40rpx;
				width: 70rpx;
				height: 70rpx;
				flex-shrink: 0;
				color: #FFFFFF;

				&.active {
					color: #0070F3;

					.itemContent {
						border: 1px solid #0070F3;
					}
				}

				.itemContent {
					border-radius: 10rpx;
					padding: 10rpx;
					border: 1px solid #FFFFFF;
					font-size: 16rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					white-space: nowrap;
				}

				.ratio-1-1 {
					width: 70%;
					height: 70%;
				}

				.ratio-16-9 {
					width: 100%;
					height: 56.25%;
				}

				.ratio-9-16 {
					width: 56.25%;
					height: 100%;
				}

				.ratio-4-3 {
					width: 100%;
					height: 75%;
				}

				.ratio-3-4 {
					width: 75%;
					height: 100%;
				}

				.ratio-3-2 {
					width: 100%;
					height: 66.6%;
				}

				.ratio-2-3 {
					width: 66.6%;
					height: 100%;
				}
			}
		}

		.btnGroup {
			display: flex;
			align-items: center;
			justify-content: space-around;
			background-color: #000000;
			height: calc(100rpx + env(safe-area-inset-bottom));
			padding-bottom: 20rpx;
			padding-top: 20rpx;
			width: 100%;

			.btn {
				width: 300rpx;
				background-color: #007AFF;
				color: #FFFFFF;
				border-radius: 99px;
				text-align: center;
				color: #FFFFFF;
				line-height: 70rpx;

				&.choose {
					margin-right: 20rpx;
					background-color: #F0AD4E;
				}
			}
		}
	}
</style>
